package cn.cloudtogo.server.mysql.controller;

import cn.cloudtogo.common.types.RequestHeaderObject;
import cn.cloudtogo.common.types.Result;
import cn.cloudtogo.support.mysql.schema.SchemaApi;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

import static cn.cloudtogo.server.WebResponse.*;

/**
 * @author yaolianhua789@gmail.com
 **/
@RestController
@RequestMapping("/schemas")
public class SchemasController {

    private final SchemaApi schemaApi;

    public SchemasController(SchemaApi schemaApi) {
        this.schemaApi = schemaApi;
    }

    @PostMapping
    public ResponseEntity<Result<Void>> create(@RequestParam String name,
                                               @RequestParam(required = false, value = "useIfNotExist") boolean useIfNotExist,
                                               @ApiIgnore RequestHeaderObject object) {
        schemaApi.create(name, useIfNotExist, object);
        return created();
    }

    @DeleteMapping("/{schema}")
    public ResponseEntity<Result<Void>> delete(@PathVariable String schema,
                                               @ApiIgnore RequestHeaderObject object) {
        schemaApi.delete(schema, object);
        return accepted();
    }

    @GetMapping
    public ResponseEntity<Result<List<String>>> list(@ApiIgnore RequestHeaderObject requestHeaderObject) {
        List<String> schemas = schemaApi.list(requestHeaderObject);
        return ok(schemas);
    }

}
